package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlaverMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishFlavorService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishFlaverMapper dishFlaverMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void add(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
//        Integer dishID = dishMapper.getDishByID(dish.getId());
        dishMapper.insert(dish);
        List<DishFlavor> flavors = dto.getFlavors();
        if (!flavors.isEmpty()) {
            flavors.forEach(flavor -> flavor.setDishId(dish.getId()));
            dishFlavorService.insert(flavors);
        }
    }

    @Override
    public PageResult page(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> dishVO = dishMapper.page(dto);
        return new PageResult(dishVO.getTotal(), dishVO.getResult());
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        Integer DishCount = dishMapper.getDishsCountByIds(ids, StatusConstant.ENABLE);
        if (DishCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        Integer SetmealDishCount = setmealDishMapper.getSetmealDishByDishIds(ids);
        if (SetmealDishCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        dishMapper.deleteDishs(ids);
        dishFlaverMapper.deleteDishFlaver(ids);
        setmealDishMapper.deleteByDishsId(ids);
    }

    @Override
    public DishVO getDishById(Long id) {
        DishVO dishVO = new DishVO();
        Dish dish = dishMapper.getDishById(id);
        BeanUtils.copyProperties(dish, dishVO);
        List<DishFlavor> dishFlavorList = dishFlaverMapper.getDishFlaversById(id);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }


    @Override
    public void update(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.update(dish);
        List<Long> dishIDList = new ArrayList<>();
        dishIDList.add(dish.getId());
        dishFlaverMapper.deleteDishFlaver(dishIDList);
        List<DishFlavor> flavors = dto.getFlavors();
        flavors.forEach(flavor -> flavor.setDishId(dto.getId()));
        dishFlaverMapper.insert(flavors);
    }

    @Override
    public List<Dish> getDishsByCategoryId(Integer cId) {
        return dishMapper.getDishByCategoryId(cId);
    }
}
